﻿using Common.Logging;
using DataCenter.Model.Interface;
using DataCenter.Provider;
using System;
using System.Reflection;
using System.ServiceProcess;

namespace DataCenter.ServiceBus.Core
{
    public class ServiceBusBase : ServiceBase
    {
        protected bool isRunning = false;
        protected static readonly ILog Logger = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
        public IProviderFactory Provider { get; private set; }

        public virtual void OnStart()
        {

        }
         
        public ServiceBusBase()
        {
            if (this.Provider == null) this.Provider = new ProviderFactory();
        }

        protected override void OnStart(string[] args)
        {
            try
            {
                isRunning = true;
                OnStart();
            }
            catch (Exception ex)
            {
                Logger.Fatal(ex.Message, ex);
                Stop();
            }
        }

        protected override void OnStop()
        {
            try
            {
                isRunning = false;
            }
            catch (Exception ex)
            {
                Logger.Warn(ex.Message, ex);
            }
        }
    }


}